import {
    ArrayNotEmpty,
    IsArray,
    IsNotEmpty,
    IsNumber,
    IsOptional,
    IsString,
} from 'class-validator';
import { IsSqlOrder, IsSqlWhere } from '../validator/sqlquery.validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ObjectLiteral } from 'typeorm';
import { Type } from 'class-transformer';

export interface UpdateDto {
    id?: number;
    primary_cod?: any;  // 可以覆盖组合主键
}

export class DeleteDto {
    @IsArray({
        message: 'ids参数类型错误',
    })
    @ArrayNotEmpty({
        message: 'ids参数不能为空',
    })
    @IsNumber(
        {},
        {
            each: true,
            message: 'ids参数类型错误',
        },
    )
    @ApiProperty({
        description: '删除id数组',
        type: 'array',
        items: {
            type: 'number',
        },
    })
    ids: number[];
}
export class DeleteStrPrimaryDto {
    /* @IsArray({
        message: 'ids参数类型错误',
    })
    @ArrayNotEmpty({
        message: 'ids参数不能为空',
    })
    @IsString({
        each: true,
        message: 'ids参数类型错误',
    }) */
    @IsArray() // 确保数组中的每个元素都是字符串
    @IsNotEmpty() // 确保数组不为空
    @Type(() => String) // 使用class-transformer确保类型安全
    @ApiProperty({
        description: '删除主键数组',
        type: 'array',
        items: {
            type: 'string',
        },
    })
    public ids!: string[];
}
export class DeletePrimaryObjDto {
    @IsArray() // 确保数组中的每个元素都是字符串
    @IsNotEmpty() // 确保数组不为空

    @ApiProperty({
        description: '删除主键数组',
        type: 'array',
        items: {
            type: 'object',
        },
    })
    public ids!: any[];
}


export type FormFieldSelectOption = {
    value: string | number;
    label: string;
    [key: string]: any;
}

export enum SqlOrderEnum {
    asc = 'asc',
    desc = 'desc',
    ASC = 'ASC',
    DESC = 'DESC',
}

type SqlOrder = {
    [key: string]: SqlOrderEnum;
};

export enum SqlWhereConditionEnum {
    equals = 'equals',
    contains = 'contains',
    not = 'not',
    between = 'between',
    in = 'in',
    notIn = 'notIn',
    lt = 'lt',
    lte = 'lte',
    gt = 'gt',
    gte = 'gte',
}
type SqlWhereItem = {
    [key: string]: {
        // 字段
        [key: string]: any; // 条件
    };
};

type relationItem = {
    // mapToProperty: string, 
    property: string, 
    alias: string, 
    condition?: string,
    parameters?: ObjectLiteral
    
};

export class SqlQueryDto {
    @IsOptional()
    @IsArray()
    @IsString({
        each: true,
    })
    @ApiPropertyOptional({
        description: '选择结果包含字段',
    })
    select?: Array<string>;

    left?: Array<relationItem>;
    right?: Array<relationItem>;

    @IsOptional()
    @IsNumber(
        {},
        {
            message: 'skip必须为数字',
        },
    )
    @ApiPropertyOptional({
        description: '跳过数量',
    })
    skip?: number;

    @IsOptional()
    @IsNumber(
        {},
        {
            message: 'take必须为数字',
        },
    )
    @ApiPropertyOptional({
        description: '返回数量',
    })
    take?: number;

    @IsOptional()
    @IsSqlOrder({
        message: '排序参数错误',
    })
    @ApiPropertyOptional({
        description: '排序参数, 枚举值 asc、 desc',
        example: {
            name: 'asc',
            age: 'desc',
        },
    })
    order?: SqlOrder;

    @IsOptional()
    @IsSqlWhere({
        message: '条件参数错误',
    })
    @ApiPropertyOptional({
        description:
            '条件参数 枚举值 equals contains not between in notIn lt lte gt gte',
        example: {
            equals: { name: 'xxx' },
            in: { age: [1, 2, 3] },
        },
    })
    where?: SqlWhereItem;

    @IsOptional()
    @IsArray()
    @IsString({
        each: true,
    })
    @ApiPropertyOptional({
        description: '选择结果group by字段',
    })
    group?: Array<string>;

    excelColumn?: Array<string>;
}

export class ResponseJson<T = null, P = null> {
    @ApiProperty({ description: '状态码' })
    code: number;

    @ApiPropertyOptional({ description: '消息数组' })
    msg?: string[];

    obj?: T;

    ext?: P;
}
